package org.example.projectdemo.controller;

import jakarta.validation.Valid;
import org.example.projectdemo.model.entity.Student;
import org.example.projectdemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * ClassName: StudentController
 * Package: org.example.projectdemo.controller
 * Description:
 *
 * @Author ZY
 * @Create 00:03
 * @Version 1.0
 */

/**
 * 学生管理控制器
 * 处理学生相关的HTTP请求
 */
@RestController
@RequestMapping("/api/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 创建学生信息
     *
     * @param student 学生信息
     * @return 创建后的学生信息
     */
    @PostMapping
    public ResponseEntity<Student> createStudent(@Valid @RequestBody Student student) {
        Student savedStudent = studentService.createStudent(student);
        return ResponseEntity.ok(savedStudent);
    }

    /**
     * 更新学生信息
     *
     * @param id 学生ID
     * @param student 学生信息
     * @return 更新后的学生信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Student> updateStudent(@PathVariable Long id, 
                                                @Valid @RequestBody Student student) {
        student.setId(id);
        try {
            Student updatedStudent = studentService.updateStudent(student);
            return ResponseEntity.ok(updatedStudent);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 根据ID获取学生信息
     *
     * @param id 学生ID
     * @return 学生信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
        Optional<Student> student = studentService.getStudentById(id);
        return student.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 获取所有学生列表
     *
     * @return 所有学生列表
     */
    @GetMapping
    public ResponseEntity<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return ResponseEntity.ok(students);
    }

    /**
     * 根据学号获取学生信息
     *
     * @param studentNumber 学号
     * @return 学生信息
     */
    @GetMapping("/by-number/{studentNumber}")
    public ResponseEntity<Student> getStudentByNumber(@PathVariable String studentNumber) {
        Optional<Student> student = studentService.getStudentByNumber(studentNumber);
        return student.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 根据班级获取学生列表
     *
     * @param className 班级名称
     * @return 该班级的学生列表
     */
    @GetMapping("/by-class/{className}")
    public ResponseEntity<List<Student>> getStudentsByClass(@PathVariable String className) {
        List<Student> students = studentService.getStudentsByClass(className);
        return ResponseEntity.ok(students);
    }

    /**
     * 根据状态获取学生列表
     *
     * @param state 学生状态 (1-在校, 0-离校)
     * @return 符合状态的学生列表
     */
    @GetMapping("/by-state/{state}")
    public ResponseEntity<List<Student>> getStudentsByState(@PathVariable Byte state) {
        List<Student> students = studentService.getStudentsByState(state);
        return ResponseEntity.ok(students);
    }

    /**
     * 软删除学生信息
     *
     * @param id 学生ID
     * @return 删除成功返回200
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable Long id) {
        if (!studentService.existsStudentById(id)) {
            return ResponseEntity.notFound().build();
        }
        studentService.deleteStudent(id);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 永久删除学生信息
     *
     * @param id 学生ID
     * @return 删除成功返回200
     */
    @DeleteMapping("/{id}/permanent")
    public ResponseEntity<Void> permanentDeleteStudent(@PathVariable Long id) {
        if (!studentService.existsStudentById(id)) {
            return ResponseEntity.notFound().build();
        }
        studentService.permanentDeleteStudent(id);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 恢复已删除的学生
     *
     * @param id 学生ID
     * @return 恢复成功返回200
     */
    @PutMapping("/{id}/restore")
    public ResponseEntity<Void> restoreStudent(@PathVariable Long id) {
        studentService.restoreStudent(id);
        return ResponseEntity.ok().build();
    }
}